<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: CreateHelpPages.php 377 2010-09-10 08:14:28Z steen.rabol $
 *
 */


Prado::using('Application.Common.Data.PortalHelpRecord');
Prado::using('Application.Common.Data.PortalPortletRecord');

class CreateHelpPages extends PortalAdminPage
{
	public static function ToolMenuName()
	{
		return Prado::localize('Create help pages');
	}

	public function onLoad($param)
	{
		parent::onLoad($param);

		$this->PageTitle = Prado::localize("Create help pages");

		if(!$this->Page->IsPostBack)
		{
			$this->createStart->Visible			= true;
			$this->LanguageList->PromptText		= Prado::localize('Select');
			$this->LanguageList->PromptValue	= "0";
			$this->LanguageList->DataSource		= $this->DataAccess->createCommand("select name,culture from tbllanguages where active=1 order by name")->query();
			$this->LanguageList->dataBind();
		}
	}

	public function CreatePages($sender, $param)
	{
		if($this->IsValid)
		{
			$this->createStart->Visible	= false;
			$this->createDone->Visible = true;

			$culture	= $this->LanguageList->SelectedValue;

			// Get system pages

			$sPages	= PortalUtil::AvailablePagesInPath('System');
			$aPages	= PortalUtil::AvailablePagesInPath('Admin');
			$sPages	= array_merge($sPages,$aPages);

			if(TPropertyValue::ensureInteger($this->DeleteFirst->Checked))
			{
				$sql = "delete from tblhelp where culture='$culture'";
				try
				{
					$this->DbConnection->createCommand($sql)->execute();
				}
				catch(Exception $e)
				{
					$this->reportError(1,$e->getMessage());
				}
			}

			$p = PortalPortletRecord::finder()->findAll();

			if($p)
			{
				foreach($p as $pp)
				{
					$sPages[] = array('name' => 'Portlet.' . $pp->name);
				}
			}

			if(count($sPages))
			{
				foreach($sPages as $sPage)
				{
					$hp = PortalHelpRecord::finder()->findBypagename_AND_culture($sPage['name'],$culture);
					if(!$hp)
					{
						$hp = new PortalHelpRecord();
						$hp->pagename	= $sPage['name'];
						$hp->culture	= $culture;
						$hp->title		= $sPage['name'];
						$hp->content	= 'Auto genereated';
						$hp->save();
					}
				}
			}
		}
	}
}

?>